<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateConversationMessagesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('conversation_messages', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('session_id');
            $table->enum('sender_type', ['user', 'ai']);
            $table->text('message');
            $table->unsignedTinyInteger('emotion_score')->nullable()->comment('当前消息的情感分数');
            $table->unsignedInteger('response_time_ms')->nullable()->comment('响应时间（毫秒）');
            $table->boolean('is_highlighted')->default(false)->comment('是否为高亮话术');
            $table->enum('highlight_type', ['best', 'error', 'neutral'])->nullable();
            $table->timestamp('created_at')->useCurrent();
            
            // 外键约束
            $table->foreign('session_id')->references('id')->on('training_sessions')->onDelete('cascade');
            
            // 索引
            $table->index(['session_id']);
            $table->index(['created_at']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('conversation_messages');
    }
}